package com.zqx.controller;

import com.zqx.constant.MessageConstant;
import com.zqx.entity.PageResult;
import com.zqx.entity.QueryPageBean;
import com.zqx.entity.Result;
import com.zqx.pojo.CheckItem;
import com.zqx.service.CheckitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author:ZQX
 * @Date:2021/8/27 17:45
 * @Description:
 */
@Controller
@ResponseBody
@RequestMapping("/checkitem")
public class CheckitemController {
    @Autowired
    private CheckitemService cs;

    /**
     * 添加检查项数据
     *
     * @param checkItem
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            //写一个service的接口,调用它的方法,不用返回值?
            cs.add(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }


    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping(value = "/findPage", method = RequestMethod.POST)
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        //调用service层的findPage,形参为QueryPageBean对象,返回值为PageResult对象
        System.out.println("调用了CheckitemController的findPage方法");
        PageResult pageResult = cs.findPage02(queryPageBean);
        System.out.println("pageResult的数据为:" + pageResult);
        if (pageResult != null) {
            return pageResult;
        } else {
            return null;
        }
    }


    /**
     * 删除检查项
     *
     * @param checkitemId
     * @return
     */
    @RequestMapping(value = "/deleteById", method = RequestMethod.GET)
    public Result deleteById(Integer checkitemId) {
        try {
            //写一个service的接口,调用它的方法,不用返回值?
            cs.deleteById(checkitemId);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    /**
     * 根据checkitemId查询t_checkitem的数据
     *
     * @param checkItemId 查询条件id
     * @return
     */
    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    public Result findById(Integer checkItemId) {
        try {
            //获取查询到的CheckItem对象
            CheckItem checkItem = cs.findById(checkItemId);
            System.out.println("checkItem的数据为" + checkItem);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS, checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    /**
     * 修改t_checkitem的数据
     *
     * @param checkItem
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody CheckItem checkItem) {
        try {
            System.out.println("checkItem的数据为" + checkItem);
            cs.update(checkItem);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    /**
     * 查询所有的检查项
     * @return
     */
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Result findAll() {
        //获取查询t_checkitem的所有数据
        List<CheckItem> list = cs.findAll();
        //如果有数据存在,Result为true
        if (list != null && list.size() > 0) {
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }


}
